package pe.com.bbva.ge.vista.vaadin.ui.registrosolicitud;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;

import org.springframework.beans.factory.annotation.Autowired;

import pe.com.bbva.ge.domain.configuracion.Lista;
import pe.com.bbva.ge.domain.solicitud.Destino;
import pe.com.bbva.ge.domain.solicitud.DetalleMaterial;
import pe.com.bbva.ge.negocio.SolicitudNegocio;
import pe.com.bbva.ge.vista.vaadin.ui.VentanaUI;
import pe.com.bbva.ge.vista.vaadin.ui.component.ComboBoxValor;
import pe.com.bbva.ge.vista.vaadin.ui.component.TxtNumerico;
import pe.com.bbva.ge.vista.vaadin.ui.utileria.AsignarDestino;
import pe.com.bbva.ge.vista.vaadin.ui.utileria.bean.PropiedadesTxtNumerico;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

public class RegistrarDetalleMaterialUI extends VentanaUI implements ClickListener, AsignarDestino {

	@AutoGenerated
	private VerticalLayout mainLayout;

	@AutoGenerated
	private Table tblDetalleMateriales;

	@AutoGenerated
	private VerticalLayout lytBotonera;

	@AutoGenerated
	private Button btnAgregarMateriales;

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	/**
	 * 
	 */
	private static final long serialVersionUID = 6515455069390543275L;
	
	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@Autowired
	private SolicitudNegocio gestionarSolicitudNegocio;
	
	private static final String COLUMNA_MATERIAL = "material";
	private static final String COLUMNA_CANTIDAD = "cantidadSolicitado";
	private static final String COLUMNA_ACCIONES = "acciones";
	
	private IndexedContainer container;

	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	
	public RegistrarDetalleMaterialUI() {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		
		buildContainer();
		
		agregarListeners();
	}

	private void buildContainer() {
		
		this.container = new IndexedContainer();
		this.container.addContainerProperty(COLUMNA_MATERIAL, ComboBoxValor.class,  null);
		this.container.addContainerProperty(COLUMNA_CANTIDAD, TxtNumerico.class,  null);
		this.container.addContainerProperty(COLUMNA_ACCIONES, Button.class,  null);
		this.tblDetalleMateriales.setContainerDataSource(container);
		
		this.tblDetalleMateriales.setColumnHeader(COLUMNA_MATERIAL, "Material a solicitar");
		this.tblDetalleMateriales.setColumnHeader(COLUMNA_CANTIDAD, "Cantidad <br/> Solicitado");
		this.tblDetalleMateriales.setColumnHeader(COLUMNA_ACCIONES, "Acciones");
		
		this.tblDetalleMateriales.setColumnWidth(COLUMNA_MATERIAL, 350);
		this.tblDetalleMateriales.setColumnWidth(COLUMNA_CANTIDAD, 90);
		this.tblDetalleMateriales.setColumnWidth(COLUMNA_ACCIONES, 60);
		
		this.tblDetalleMateriales.setStyleName("tbl-header-multi-row");
		
	}

	private void agregarListeners() {
		btnAgregarMateriales.addListener((ClickListener)this);
	}

	@Override
	public void postConstruccion() {
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		if(event.getButton().equals(btnAgregarMateriales))
		{
			agregarMaterial();
		}
		else if(event.getButton().getData() != null)
		{
			Object itemId = event.getButton().getData();
			quitarMaterial(itemId);
		}
	}
	
	private void agregarMaterial()
	{
		
		int itemIdNro = container.size(); 
		Item item = container.addItem(itemIdNro);
		
		agregarControles(itemIdNro, item);
	}
	
	private void agregarControles(int itemIdNro, Item item)
	{
		PropiedadesTxtNumerico propTxtNEnteros = new PropiedadesTxtNumerico();
		propTxtNEnteros.setEsDecimal(false);
		
		final ComboBoxValor cmbMateriales = new ComboBoxValor(Lista.CODIGO.MATERIAL);
		cmbMateriales.setWidth("335");	
		
		TxtNumerico txtCantidadSolicitado = new TxtNumerico("", propTxtNEnteros);
		txtCantidadSolicitado.setWidth("75");
		txtCantidadSolicitado.setValue("0");
		
		final  Button btnAcciones = new Button();
		btnAcciones.setData(itemIdNro);
		btnAcciones.setCaption("X");
		
		btnAcciones.addListener((ClickListener)this);
		//item.
		item.getItemProperty(COLUMNA_MATERIAL).setValue(cmbMateriales);
		item.getItemProperty(COLUMNA_CANTIDAD).setValue(txtCantidadSolicitado);
		item.getItemProperty(COLUMNA_ACCIONES).setValue(btnAcciones);
	}
	
	private void quitarMaterial(Object itemId)
	{
		this.container.removeItem(itemId);
		this.tblDetalleMateriales.removeItem(itemId);
	}

	public Table getTblDetalleMateriales() {
		return tblDetalleMateriales;
	}

	public void setTblDetalleMateriales(Table tblDetalleMateriales) {
		this.tblDetalleMateriales = tblDetalleMateriales;
	}
	
	@Override
	public void asignarDestino(Destino destino) {
		Iterator<?> iter = container.getItemIds().iterator();
		
		destino.setDetalleMateriales(new ArrayList<DetalleMaterial>());
		
		DetalleMaterial detalleMaterial;
		
		while (iter.hasNext()) {
			Object objectId = (Object) iter.next();
			Item item = container.getItem(objectId);
			TxtNumerico txt = (TxtNumerico) item.getItemProperty(COLUMNA_CANTIDAD).getValue();
			ComboBoxValor cmb = (ComboBoxValor) item.getItemProperty(COLUMNA_MATERIAL).getValue();
			
			detalleMaterial = new DetalleMaterial();
			detalleMaterial.setSolicitado(new BigInteger((String) txt.getValue()));
			detalleMaterial.setTipoMaterial(cmb.obtenerValor());
			
			destino.addDetalleMaterial(detalleMaterial);			
		}
	}
	
	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// tblDetalleMateriales
		tblDetalleMateriales = buildTblDetalleMateriales();
		mainLayout.addComponent(this.tblDetalleMateriales);
		mainLayout.setExpandRatio(this.tblDetalleMateriales, 1.0f);	
		
		// lytBotonera
		lytBotonera = buildLytBotonera();
		mainLayout.addComponent(lytBotonera);
		
		return mainLayout;
	}

	@AutoGenerated
	private Table buildTblDetalleMateriales() {
		
		tblDetalleMateriales = new Table();
		tblDetalleMateriales.setImmediate(false);
		tblDetalleMateriales.setWidth("780px");
		tblDetalleMateriales.setHeight("300px");	
		
		return tblDetalleMateriales;
	}

	@AutoGenerated
	private VerticalLayout buildLytBotonera() {
		// common part: create layout
		lytBotonera = new VerticalLayout();
		lytBotonera.setImmediate(false);
		lytBotonera.setWidth("780px");
		lytBotonera.setHeight("40px");
		lytBotonera.setMargin(false);
		
		// btnAgregarMateriales
		btnAgregarMateriales = new Button();
		btnAgregarMateriales.setCaption("+ Agregar Materiales");
		btnAgregarMateriales.setImmediate(true);
		btnAgregarMateriales.setWidth("-1px");
		btnAgregarMateriales.setHeight("-1px");
		lytBotonera.addComponent(btnAgregarMateriales);
		lytBotonera.setComponentAlignment(btnAgregarMateriales, new Alignment(
				34));
		
		return lytBotonera;
	}

}
